package com.qd.qx.im.imui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.qd.common.commonutils.ToastUitl;
import com.qd.qx.R;
import com.qd.qx.api.MyXMPPTCPConnection;
import com.qd.qx.bean.UserData;
import com.qd.qx.ui.module1.MessageFragment;
import com.qd.qx.ui.module2.TongXunLuSelectActivity;

import org.jivesoftware.smackx.muc.MultiUserChat;

/**
 * Code is far away from bug with the animal protecting
 * Author：YS on 2018/10/18
 * Function：消息菜单功能
 */
public class ImMenuPopwindow extends PopupWindow implements View.OnClickListener {
    private Context context;
    private LinearLayout groupChat, saoyisao, touping, yaoyiyao, markMessage, xiuxiOver;
    private MessageFragment messageFragment;

    public ImMenuPopwindow(Context context, MessageFragment messageFragment) {
        super(context);
        this.context = context;
        this.messageFragment = messageFragment;
        initalize();
    }

    private void initalize() {
        View view = View.inflate(context, R.layout.pop_immenu, null);
        groupChat = view.findViewById(R.id.ll_groupchat);
        saoyisao = view.findViewById(R.id.ll_saoyisao);
        touping = view.findViewById(R.id.ll_touping);
        yaoyiyao = view.findViewById(R.id.ll_yaoyiyao);
        markMessage = view.findViewById(R.id.ll_markmessage);
        xiuxiOver = view.findViewById(R.id.ll_xiuxiover);

        groupChat.setOnClickListener(this);
        saoyisao.setOnClickListener(this);
        touping.setOnClickListener(this);
        yaoyiyao.setOnClickListener(this);
        markMessage.setOnClickListener(this);
        xiuxiOver.setOnClickListener(this);

        setContentView(view);
        initWindow();
    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth((int) (d.widthPixels * 0.4));//0.35
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //  this.setAnimationStyle(R.style.up_down_popwindow_anim_style);//设置动画
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 40);
        // showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_groupchat:
                TongXunLuSelectActivity.startAction((Activity) context, "0", "通讯录");
                break;
            case R.id.ll_saoyisao:
                ToastUitl.showShort("扫一扫");
                break;
            case R.id.ll_touping:
                ToastUitl.showShort("投屏");
                break;
            case R.id.ll_yaoyiyao:
                ToastUitl.showShort("摇一摇");
                break;
            case R.id.ll_markmessage:
                messageFragment.markAllUnreadCount();
                break;
            case R.id.ll_xiuxiover:
                ToastUitl.showShort("休息结束");
                break;
            default:
                break;
        }
        this.dismiss();
    }

}